package huoye.shops.huoye.operation.activity;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.huoye.operation.model.VipGustsData;
import huoye.shops.huoye.operation.presenter.NewAddGuestsPresenter;
import huoye.shops.huoye.operation.view.INewAddGuestsView;
import huoye.shops.widget.RoundImageView;

public class NewAddGuestsActivity extends BaseActivity implements INewAddGuestsView{

    @BindView(R.id.new_add_guests_et_seek)
    EditText etSeek;

    @BindView(R.id.new_add_guests_ll)
    LinearLayout ll;
    @BindView(R.id.new_add_guests_rv_head)
    RoundImageView rvHead;

    @BindView(R.id.new_add_guests_tv_name)
    TextView tvName;
    @BindView(R.id.new_add_guests_tv_phone)
    TextView tvPhone;

    @OnClick(R.id.new_add_guests_btn_select)
    void onSelectClick(){
        mVipGustsData.setEdit(false);
        startActivity(GuestsSetActivity.class,mVipGustsData);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_new_add_guests;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("新增嘉宾");
        etSeek.setHint("搜索手机号");
        NewAddGuestsPresenter mNewAddGuestsPresenter = new NewAddGuestsPresenter();
        mNewAddGuestsPresenter.addTaskListener(this);
        mNewAddGuestsPresenter.onStart();

        etSeek.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {

                /*判断是否是“GO”键*/
                if((actionId == 0 || actionId == 3) && keyEvent != null){
                    /*隐藏软键盘*/
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(
                                v.getApplicationWindowToken(), 0);
                    }

                   // makeToast("你点击了确定键");
                    String seek = etSeek.getText().toString();
                    mNewAddGuestsPresenter.searchUser(seek);


                    return true;
                }

                return false;
            }
        });

    }


    VipGustsData mVipGustsData;
    @Override
    public void seekGuests(VipGustsData vipGustsData) {
        if(StringUtils.isNotEmpty(vipGustsData.getNickName())){
            ll.setVisibility(View.VISIBLE);
            this.mVipGustsData = vipGustsData;
            Glide.with(this)
                    .load(vipGustsData.getHeadImg())
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .into(rvHead);
            tvName.setText(vipGustsData.getNickName());
            tvPhone.setText(vipGustsData.getMobile());
        }else {
            makeToast("未搜索到该嘉宾");
        }

    }
}
